import javax.swing.*;
import java.util.Scanner;

public class Day13 {

        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int T = in.nextInt();
                while(T != 0){
                    int n = in.nextInt(),k = in.nextInt();
                    String s = in.next();
                    char[] ele = new char[n];
                    int sum = 0;
                    for(int i = 0; i < n ; i++){
                        ele[i] = s.charAt(i);
                    }
                    for(int i = 0;i < n ;i ++){
                        if(ele[i] == 'L'){
                            sum--;
                        }else{
                            if(i <= n - 3){
                                if(ele[i] == 'W' && ele[i+1] == 'W' && ele[i+2] == 'W'){
                                    sum += k;
                                    continue;
                                }
                            }
                            sum++;
                        }
                    }
                    System.out.println(sum);

                }
            }
        }


    public int maxLength (int[] arr) {
        // write code here
        int len = 0;
        int[] underArr = {arr.length};
        int i = 0,j = 0;
        while(i < arr.length){
            if(i >= i+1){
                i++;
                if(j != 0){
                    j = i;
                }
            }else{
                i++;
            }
        }
        len = i - j;
        return len;
    }

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String s = in.next();
            char[] ele = new char[n];
            for(int i = 0;i < n;i++){
                ele[i] = s.charAt(i);
            }
            sort(ele);


        }
    }

    public static void sort(char[] e){
        for(int i = 0;i < e.length ; i++){
            for(int j = 0 ; j < e.length - 1 - i; j ++){
                if(e[j] > e[j + 1]){
                    char tmp = e[j];
                    e[j] = e[j+1];
                    e[j+1] = tmp;
                }
            }
        }
    }
}
